package configrations

import "fmt"

// 配置文件结构
type BasicConfig struct {
	Application ApplicationConfig `yaml:"application"`
	Database    struct {
		Mysql MysqlConfig `yaml:"mysql,omitempty"`
	} `yaml:"database,omitempty"`
	Cache struct {
		LRU LruConfig `yaml:"lru,omitempty"`
	} `yaml:"cache,omitempty"`
}

// 应用配置
type ApplicationConfig struct {
	Name string `yaml:"name"`
	Port int    `yaml:"port"`
}

// LRU缓存配置
type LruConfig struct {
	MaxSize int `yaml:"max_size"`
}

// MySQL数据库配置
type MysqlConfig struct {
	Datasource Datasource `yaml:"datasource"`
}

type Datasource struct {
	Host            string `yaml:"host"`
	Port            int    `yaml:"port"`
	User            string `yaml:"user"`
	Password        string `yaml:"password"`
	Database        string `yaml:"database"`
	Charset         string `yaml:"charset"`
	ParseTime       bool   `yaml:"parse_time"`
	Loc             string `yaml:"loc"`
	MaxIdleConns    int    `yaml:"max_idle_conns"`
	MaxOpenConns    int    `yaml:"max_open_conns"`
	ConnMaxLifetime int    `yaml:"conn_max_life_time"`
	ConnMaxIdleTime int    `yaml:"conn_max_idle_time"`
}

func (d Datasource) ToDNS() string {
	dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=%v&loc=%s",
		d.User, d.Password, d.Host, d.Port, d.Database, d.Charset, d.ParseTime, d.Loc)
	return dns
}
